﻿namespace game.character.battle
{

    /// <summary>
    /// 
    /// </summary>
    public class GameBattler
    {

        /// <summary>
        /// 
        /// </summary>
        public enum Status
        {
            Normal,
            Poisoned,
            Invincible,
            Dead
        }

        #region Attributes

        /// <summary>
        /// Health Points
        /// </summary>
        protected float _HP;

        /// <summary>
        /// Health Points (get / set)
        /// </summary>
        public float HP
        {
            get { return _HP; }
            protected set
            {
                _HP = (int)Microsoft.Xna.Framework.MathHelper.Clamp(value, 0, _MaxHP);
                if (_HP == 0)
                    _Status = Status.Dead;
            }
        }

        /// <summary>
        /// Max Health Points
        /// </summary>
        protected float _MaxHP;

        /// <summary>
        /// Math Health Points (get / set)
        /// </summary>
        public float MaxHP
        {
            get { return _MaxHP; }
            protected set
            {
                _MaxHP = System.Math.Max(0, value);
                HP = _HP;
            }
        }

        public float HitRate;

        /// <summary>
        /// Status
        /// </summary>
        protected Status _Status = Status.Normal;

        /// <summary>
        /// Check if the character has a certain status
        /// </summary>
        /// <param name="status">Status</param>
        /// <returns>If the character has it or not</returns>
        public bool HasStatus(Status status) { return _Status.HasFlag(status); }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        public void ApplyStatus(Status status)
        {
            _Status |= status;
        }

        public void RemoveStatus(Status status)
        {
            _Status &= ~status;
        }

        #endregion

        public GameBattler(float maxHP)
        {
            _MaxHP = maxHP;
            HP = maxHP;
        }

        public GameBattler(float hp, float maxHP)
        {
            _MaxHP = maxHP;
            HP = hp;
        }

        public void DecreaseMaxHP(float value) { MaxHP -= System.Math.Abs(value); }

        public void IncreaseMaxHP(float value) { MaxHP += System.Math.Abs(value); }

        public bool DecreaseHP(float value)
        {
            if (_Status.HasFlag(Status.Invincible))
                return false;
            HP -= System.Math.Abs(value);
            return true;
        }

        public void IncreaseHP(float value) { HP += System.Math.Abs(value); }

        public void RecoverAll()
        {
            _HP = _MaxHP;
            _Status = Status.Normal;
        }

        public void Hit(GameBattler other)
        {
            other.DecreaseHP(HitRate);
        }
        public void ContinuousHit(GameBattler other, float Miliseconds)
        {
            other.DecreaseHP((HitRate * Miliseconds / 1000.0f));
        }

        public bool Kill()
        {
            if (_Status.HasFlag(Status.Invincible))
                return false;
            HP = 0;
            return true;
        }

        public bool IsDead
        {
            get { return _HP == 0 && !_Status.HasFlag(Status.Invincible); }
        }

    }

}